package com.guaju.yueshangclothing.fragment.home;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.guaju.yueshangclothing.BR;
import com.guaju.yueshangclothing.R;
import com.guaju.yueshangclothing.adapter.CommonRecyclerViewAdapter;
import com.guaju.yueshangclothing.base.BaseFragment;
import com.guaju.yueshangclothing.bean.Goods;
import com.guaju.yueshangclothing.databinding.FragmentTotalgoodsBinding;
import com.guaju.yueshangclothing.utils.CommonUtils;

import java.util.ArrayList;

/**
 * Created by guaju on 2018/10/9.
 */

public class PaikeFragment extends BaseFragment<FragmentTotalgoodsBinding> {
    private static final String TYPE = "PaikeFragment";
    private boolean mIsPrepared;
    private boolean mIsFirst = true;


    // 开始请求的角标
    private int mStart = 0;
    // 一次请求的数量
    private int mCount = 18;
    //
    private GridLayoutManager mLayoutManager;


    ArrayList goodsList = new ArrayList<>();
    private CommonRecyclerViewAdapter<Goods> totalGoodsAdapter;

    @Override
    public int setContent() {
        return R.layout.fragment_totalgoods;
    }

    public static PaikeFragment newInstance(String param) {
        PaikeFragment fragment = new PaikeFragment();
        Bundle args = new Bundle();
        args.putString(TYPE, param);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 准备就绪
        mIsPrepared = true;
        loadData();
        showContentView();

        bindingView.srlTotalGoods.setColorSchemeColors(CommonUtils.getColor(R.color.colorTheme));
        bindingView.srlTotalGoods.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                bindingView.srlTotalGoods.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        loadCustomData();
                    }
                }, 1000);

            }
        });
        mLayoutManager = new GridLayoutManager(getActivity(), 2);
        bindingView.recyclerview.setLayoutManager(mLayoutManager);
        //recyclerview
        scrollRecycleView();
    }

    @Override
    protected void loadData() {
        if (!mIsPrepared || !mIsVisible || !mIsFirst) {
            return;
        }
        mIsFirst = false;
        bindingView.linearlayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadCustomData();
            }
        }, 500);
    }

    private void loadCustomData() {


        for (int i = 0; i < 10; i++) {
            Goods goods = new Goods();
            goodsList.add(goods);
        }
        totalGoodsAdapter = new CommonRecyclerViewAdapter<Goods>(getActivity(), getLayoutInflater(), R.layout.item_goods, BR.goods, goodsList);


        bindingView.recyclerview.setAdapter(totalGoodsAdapter);

        if (bindingView.srlTotalGoods.isRefreshing()) {
            bindingView.srlTotalGoods.setRefreshing(false);
        }


    }

    //计算recyclerview滑动，展示下拉刷新，上拉加载
    public void scrollRecycleView() {
        bindingView.recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();

                    /**StaggeredGridLayoutManager*/
//                    int[] into = new int[(mLayoutManager).getSpanCount()];
//                    lastVisibleItem = findMax(mLayoutManager.findLastVisibleItemPositions(into));

                    if (totalGoodsAdapter == null) {
                        return;
                    }
                    if (mLayoutManager.getItemCount() == 0) {
                        totalGoodsAdapter.updateLoadStatus(totalGoodsAdapter.LOAD_NONE);
                        return;

                    }
                    if (lastVisibleItem + 1 == mLayoutManager.getItemCount()
                            && totalGoodsAdapter.getLoadStatus() != totalGoodsAdapter.LOAD_MORE) {
//                        mBookAdapter.updateLoadStatus(BookAdapter.LOAD_PULL_TO);
                        // isLoadMore = true;
                        totalGoodsAdapter.updateLoadStatus(totalGoodsAdapter.LOAD_MORE);

                        //new Handler().postDelayed(() -> getBeforeNews(time), 1000);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
//                                String tag= BookApiUtils.getRandomTAG(listTag);
//                                doubanBookPresenter.searchBookByTag(BookReadingFragment.this,tag,true);
                                mStart += mCount;
                                loadCustomData();
                            }
                        }, 1000);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();

                /**StaggeredGridLayoutManager*/
//                int[] into = new int[(mLayoutManager).getSpanCount()];
//                lastVisibleItem = findMax(mLayoutManager.findLastVisibleItemPositions(into));
            }
        });
    }

}
